import request from '@/utils/request'
import { removeToken, setToken } from '@/utils/storage'
import { LOGIN, LOGIN_LOGOUT, REMOVE_INFO } from '../constants'

export const login = (formObj) => {
  return async (dispatch) => {
    const res = await request.post('/authorizations', formObj)
    console.log(res.data.data.token)
    const token = res.data.data.token

    //本地存储
    setToken(token)

    dispatch({
      type: LOGIN,
      payload: token,
    })
  }
}

export const logout = () => {
  return (dispatch) => {
    //清除本地存储
    removeToken()
    //清除用户信息
    dispatch({
      type: REMOVE_INFO,
    })

    //清除token
    dispatch({
      type: LOGIN_LOGOUT,
    })
  }
}
